package com.woniuxy.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.core.exception.platformException.PlatformException;
import com.woniuxy.core.exception.platformException.PlatformExceptionCode;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.platform.dao.PlatformAccount;
import com.woniuxy.platform.mapper.PlatformAccountMapper;
import com.woniuxy.platform.model.dto.PlatformLoginDto;
import com.woniuxy.platform.model.form.AddpaltformForm;
import com.woniuxy.platform.model.param.LoadAllpaltforminfoParam;
import com.woniuxy.platform.service.PlatformAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@Service
public class PlatformAccountServiceImpl extends ServiceImpl<PlatformAccountMapper, PlatformAccount> implements PlatformAccountService {
    @Autowired
    private PlatformAccountMapper platformAccountMapper;

    public void test() {
        System.out.println("测试成功");
        return;
    }

    @Override
    public void paltformregister(AddpaltformForm form) {
        QueryWrapper<PlatformAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("platform_account",form.getPlatformAccount());
        PlatformAccount account = platformAccountMapper.selectOne(wrapper);
        if (account!=null){
            throw new PlatformException(PlatformExceptionCode.ACCOUNT_HUAE_PEPOLEUSE);
        }
        PlatformAccount platformAccount = BeanUtil.toBean(form, PlatformAccount.class);
        platformAccount.setPlatformAddPerson(form.getPlatformAccount());

        // 获取当前时间，并格式化为指定的日期时间格式
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = currentTime.format(formatter);
        LocalDateTime parsedTime = LocalDateTime.parse(formattedTime, formatter);
        platformAccount.setPlatformTime(parsedTime);

        //0表示待审核 1表示已通过
        platformAccount.setPlatformState(0);
        platformAccount.setPlatformRoleId(1);
        platformAccountMapper.insert(platformAccount);
    }

    @Override
    public PlatformLoginDto paltformlogin(AddpaltformForm form) {
        if(form.getPlatformAccount()==null){
            throw new PlatformException(PlatformExceptionCode.PLATFORM_ACCOUNT_NOT_NULL);
        }
        if(form.getPlatformPass()==null){
            throw new PlatformException(PlatformExceptionCode.PLATFORM_PASS_NOT_NULL);
        }
        QueryWrapper<PlatformAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("platform_account",form.getPlatformAccount());
        wrapper.eq("platform_pass",form.getPlatformPass());
        PlatformAccount platformAccount = platformAccountMapper.selectOne(wrapper);
        if(platformAccount==null){
            throw new PlatformException(PlatformExceptionCode.PLATFORM_ERROR_ACCPSS);
        }
        Integer platformState = platformAccount.getPlatformState();
        System.out.println("------------");
        System.out.println(platformState);
        System.out.println("------------");
        if (platformState==0){
            throw new PlatformException(PlatformExceptionCode.PLATFORM_ADMIN_NOT_PASS);
        }
        if(platformAccount==null){
            throw new PlatformException(PlatformExceptionCode.PLATFORM_ERROR_ACCPSS);
        }
        return BeanUtil.toBean(platformAccount,PlatformLoginDto.class);
    }


    @Override
    public PageInfo<PlatformAccount> LoadALLplatform(LoadAllpaltforminfoParam param, String id) {
        Page<PlatformAccount> page = new Page<PlatformAccount>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 5 : param.getPageSize());
        QueryWrapper<PlatformAccount> wrapper = new QueryWrapper<PlatformAccount>();
        wrapper.eq("platform_state",0);
        platformAccountMapper.selectPage(page, wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setList(page.getRecords());
        pageInfo.setPages(page.getPages());
        System.out.println("----------------");
        System.out.println("----------------");
        System.out.println(pageInfo);
        System.out.println("PageInfo: ");
        System.out.println("List: " + pageInfo.getList());
        System.out.println("Pages: " + pageInfo.getPages());
        return pageInfo;
    }

    @Override
    public PlatformAccount updateplatformState(Integer id, Integer platformId) {
        System.out.println("========");
        System.out.println(platformId);
        System.out.println("========");
        QueryWrapper<PlatformAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("platform_id", platformId);
        QueryWrapper<PlatformAccount> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("platform_id",id);
        PlatformAccount account = platformAccountMapper.selectOne(wrapper1);
        String account1 = account.getPlatformAccount();
        System.out.println("-------------");
        System.out.println(account1);
        System.out.println("-------------");
        PlatformAccount platformAccount = platformAccountMapper.selectOne(wrapper);
        Integer state = platformAccount.getPlatformState();
        platformAccount.setPlatformUpdatePerson(account1);
        // 获取当前时间，并格式化为指定的日期时间格式
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = currentTime.format(formatter);
        LocalDateTime parsedTime = LocalDateTime.parse(formattedTime, formatter);
        platformAccount.setPlatformUpdateTime(parsedTime);
        //0禁用 1启用
        if (state==0){
            platformAccount.setPlatformState(1); // 将状态从0改为1
            platformAccountMapper.updateById(platformAccount); // 更新数据库中的数据
        }
        return platformAccount;
    }

    @Override
    public PlatformAccount LoadAdminByPlatformAccount(String platformAccounts) {
        QueryWrapper<PlatformAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("platform_account",platformAccounts);
        return platformAccountMapper.selectOne(wrapper);
    }

    @Override
    public PlatformAccount delplatformState(Integer id, Integer platformId) {
        System.out.println("========");
        System.out.println(platformId);
        System.out.println("========");
        QueryWrapper<PlatformAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("platform_id", platformId);
        PlatformAccount platformAccount = platformAccountMapper.selectOne(wrapper);
        if (platformAccount==null){
            throw  new PlatformException(PlatformExceptionCode.NOT_ACCOUNT);
        }
        int delete = platformAccountMapper.delete(wrapper);
        if (delete==0){
            throw  new PlatformException(PlatformExceptionCode.DEL_ERROR);
        }
        return null;
    }

    @Override
    public PageInfo<PlatformAccount> LoadALLplatforms(LoadAllpaltforminfoParam param, String id) {
        Page<PlatformAccount> page = new Page<PlatformAccount>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 5 : param.getPageSize());
        QueryWrapper<PlatformAccount> wrapper = new QueryWrapper<PlatformAccount>();
        wrapper.eq("platform_state",1);
        platformAccountMapper.selectPage(page, wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setList(page.getRecords());
        pageInfo.setPages(page.getPages());
        System.out.println("----------------");
        System.out.println("----------------");
        System.out.println(pageInfo);
        System.out.println("PageInfo: ");
        System.out.println("List: " + pageInfo.getList());
        System.out.println("Pages: " + pageInfo.getPages());
        return pageInfo;
    }


}
